package modelo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.*;
import hbt.AbstractHibernateManager;

public class PersonalDao extends AbstractHibernateManager {
	
	private static PersonalDao instancia = null;
	
	public static PersonalDao getInstancia()
	{
		if(instancia == null)
		{
			instancia = new PersonalDao();
		}
		return instancia;
	}
	
	public void save(PersonalEntity m){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(m);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[personal PERSISTED ON TABLE personal]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	public void saveOrUpdate(PersonalEntity m){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.saveOrUpdate(m);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[personal saveOrUpdate ON TABLE personal]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	
	
	public void guardar (PersonalEntity m){
		Session s = null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(m);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[personal SAVED ON TABLE personal]::OK");
		} catch (HibernateException e) {		
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
	}
	
	public void merge(PersonalEntity m){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.merge(m);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[personal MERGED ON TABLE personal]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	public PersonalEntity obtenerPersonalXLegajo(Integer legajo) {
		Session s = null;
		PersonalEntity perso = new PersonalEntity();
		
		try {
			s = getNewSession();
			perso =  (PersonalEntity) s.createQuery("FROM PersonalEntity P " +
									"WHERE P.legajo = :leg")
							  .setInteger("leg", legajo)
							  .uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return perso;
	}
	
	public PersonalEntity obtenerPersona(Integer legajo) {
		Session s = null;
		PersonalEntity perso = new PersonalEntity();
		
		try {
			s = getNewSession();
			perso =  (PersonalEntity) s.createQuery("FROM PersonalEntity P " +
									"WHERE P.legajo = :leg")
							  .setInteger("leg", legajo)
							  .uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return perso;
	}
	
	@SuppressWarnings("rawtypes")
	public List<PersonalEntity> obtenerPersonal() {
		Session s = null;
		List<PersonalEntity> lista = new ArrayList<PersonalEntity>();
		try {
			s = getNewSession();
			Iterator it = s.createQuery("FROM PersonalEntity").iterate();
			while(it.hasNext()) {
				PersonalEntity perso = (PersonalEntity) it.next();
				lista.add(perso);
			}
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return lista;
	}
	
}
